﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataTransferObject
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class MappedFreeTimeList
    {
        #region Attributes

        /// <summary>
        /// 
        /// </summary>
        private DateTime offset;
        /// <summary>
        /// 
        /// </summary>
        private List<MappedFreeTimeBlock> _listMappedFreeTimeBlock;
        /// <summary>
        /// 
        /// </summary>
        private int currentBlock;
        /// <summary>
        /// 
        /// </summary>
        private int numberOfFreeTimeBlock;

        #endregion

        #region Essential Methods

        /// <summary>
        /// Initializes a new instance of the <see cref="FreeTimeList"/> class.
        /// </summary>
        public MappedFreeTimeList()
        {
            this._listMappedFreeTimeBlock = new List<MappedFreeTimeBlock>();
            this.currentBlock = -1;
            this.numberOfFreeTimeBlock = 0;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FreeTimeList"/> class.
        /// </summary>
        /// <param name="freeTimeBlocks">The free time blocks.</param>
        public MappedFreeTimeList(List<MappedFreeTimeBlock> freeTimeBlocks)
        {
            this.currentBlock = -1;
            this._listMappedFreeTimeBlock = freeTimeBlocks;
            this.numberOfFreeTimeBlock = _listMappedFreeTimeBlock.Count;
        }

        /// <summary>
        /// Thêm MappedFreeTimeBlock vào list, 
        /// xử lí việc merge MappedFreeTimeBlock mới với MappedFreeTimeBlock cuối cùng nếu có thể merge được
        /// </summary>
        /// <param name="newMappedFTB">The free time block.</param>
        /// <returns></returns>
        public void Add(MappedFreeTimeBlock newMappedFTB)
        {
            if (ListFreeTimeBlock.Count >= 1)
            {
                MappedFreeTimeBlock lastMappedFTB = ListFreeTimeBlock.Last();
                //if (lastMappedFTB.End + 1 == newMappedFTB.Start)
                if (lastMappedFTB.End == newMappedFTB.Start)
                {
                    lastMappedFTB.End = newMappedFTB.End;
                }
                else
                {
                    this._listMappedFreeTimeBlock.Add(newMappedFTB);
                }
            }
            else
            {
                ListFreeTimeBlock.Add(newMappedFTB);
            }
        }


        /// <summary>
        /// Removes the free time block.
        /// </summary>
        /// <param name="index">The index.</param>
        public void removeFreeTimeBlock(int index)
        {
            this._listMappedFreeTimeBlock.RemoveAt(index);
        }

        /// <summary>
        /// Fists the block.
        /// </summary>
        /// <returns></returns>
        public MappedFreeTimeBlock fistBlock()
        {
            return (MappedFreeTimeBlock)this._listMappedFreeTimeBlock[0];
        }

        /// <summary>
        /// Nexts the block.
        /// </summary>
        /// <returns></returns>
        public MappedFreeTimeBlock nextBlock()
        {
            //currentBlock = (currentBlock == timeBlocks.Count - 1)?(currentBlock = 0):(currentBlock++);
            currentBlock++;
            return (MappedFreeTimeBlock)_listMappedFreeTimeBlock[currentBlock];
        }

        /// <summary>
        /// Determines whether [is any free block].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if [is any free block]; otherwise, <c>false</c>.
        /// </returns>
        public bool isAnyFreeBlock()
        {
            return (currentBlock != _listMappedFreeTimeBlock.Count - 1);
        }



        #endregion

        #region Properties

        public List<MappedFreeTimeBlock> ListFreeTimeBlock
        {
            get { return _listMappedFreeTimeBlock; }
            set { this._listMappedFreeTimeBlock = value; }
        }

        public int CurrentBlock
        {
            get { return currentBlock; }
            set { this.currentBlock = value; }
        }

        public DateTime Offset
        {
            get { return this.offset; }
            set { this.offset = value; }
        }

        #endregion

        #region bổ sung
        /* @LUÂN
         * CÁC BẠN CÓ THỂ:
         * 1. COMMENT VỀ NHỮNG BỔ SUNG GÓP Ý
         * 2. VIẾT CODE ĐỀ XUẤT TRONG REGION NÀY
         * 
        */


        #endregion
    }
}
